//
//  YSGNurseAppointmentOrderPageViewController.swift
//  EyeVisionForPatients
//
//  Created by PMC on 2022/11/25.
//  Copyright © 2022 QDSG. All rights reserved.
//

import UIKit
import YSGFramework
import JXSegmentedView

class YSGNurseAppointmentOrderPageViewController: CLViewController {
    
    init(defaultSelectedIndex: Int = 0) {
        super.init()
        segmented.defaultSelectedIndex = defaultSelectedIndex
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    let segmented = JXSegmentedView()
    let dataSource = JXSegmentedTitleDataSource()
    var viewControllers: [UIViewController & JXSegmentedListContainerViewListDelegate] = [] {
        didSet {
            dataSource.titles = viewControllers.map({ $0.title ?? "" })
        }
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        initLayout()
        initData()
    }
    func initLayout() {
        title = "护理预约"

        view.addSubview(segmented)
        segmented.snp.makeConstraints { make in
            make.left.top.right.equalToSuperview()
            make.height.equalTo(40)
        }
        segmented.backgroundColor = .white
        
        segmented.dataSource = dataSource
        dataSource.titleNormalColor = .black
        dataSource.titleSelectedColor = .mainColor
        dataSource.titleNormalFont = .systemFont(ofSize: 15)
        
        let indicator = JXSegmentedIndicatorLineView()
        indicator.indicatorWidth = 40
        indicator.indicatorColor = .mainColor
        indicator.indicatorHeight = 4
        segmented.indicators = [indicator]
        
        let listContainer = JXSegmentedListContainerView(dataSource: self)
        segmented.listContainer = listContainer
        view.addSubview(listContainer)
        listContainer.snp.makeConstraints { make in
            make.left.bottom.right.equalToSuperview()
            make.top.equalTo(segmented.snp.bottom)
        }
    }
    func initData() {
        viewControllers = [
            YSGNurseAppointmentOrderController(type: 0),
            YSGNurseAppointmentOrderController(type: 1)
        ]
        segmented.reloadData()
    }
}
extension YSGNurseAppointmentOrderPageViewController: JXSegmentedListContainerViewDataSource {
    func numberOfLists(in listContainerView: JXSegmentedListContainerView) -> Int {
        return viewControllers.count
    }
    
    func listContainerView(_ listContainerView: JXSegmentedListContainerView, initListAt index: Int) -> JXSegmentedListContainerViewListDelegate {
        return viewControllers[index]
    }
}
